<?php

namespace app\services;

/**
 * AccountModel
 * 系统用户进存明细
 */

class AccountServices
{
    protected $model = '';

    public function __construct()
    {
        $this->model = model('account_log');
    }

    // 查询当前用户账号记录
    public function account_log($where = [], $page = 1, $num = 10)
    {
        $count = $this->model->where($where)->count();
        $pageSize = $count / $num; // 最大页数
        $lim = ($page - 1) * $num; // 每次查询起始位置
        $res['data'] = $this->model->where($where)->order('id desc')->limit($lim, $num)->select();
        $res['page'] = (int)$page;
        $res['max_page'] = ceil($pageSize);
        return $res;
    }

    /**
     * @Notes 用户流水日志
     * @parser $user_id 用户id
     * @parser $price 原始金额
     * @parser $is_auto 是否自动获取原始金额 0否 1是
     * @parser $money 变动金额
     * @parser $money_type 变动金额类型 integral 积分 余额 money wxpay 微信支付 zfbpay 支付宝支付
     * @parser $desc 日志描述
     * @parser $is_up 是否增加 0否 1是
     */
    public function changeAccountLog($user_id, $price = 0, $is_auto = 0, $money = 0, $money_type = '', $desc = '', $is_up = 0)
    {
        if ($is_auto == 1) {
            $price = model('user')->where('id', 'eq', $user_id)->value('money');
        }
        $data = [
            'user_id' => $user_id,
            'price' => $price,
            'is_up' => $is_up,
            'money' => $money,
            'money_type' => $money_type,
            'desc' => $desc,
            'create_time' => time()
        ];
        return $this->model->insert($data);
    }
}
